<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="title">
      <el-input v-model="dataForm.title" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="cover">
      <el-input v-model="dataForm.cover" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="content">
      <el-input v-model="dataForm.content" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="teamid">
      <el-input v-model="dataForm.teamid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="commentCount">
      <el-input v-model="dataForm.commentCount" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="collectionCount">
      <el-input v-model="dataForm.collectionCount" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="supportCount">
      <el-input v-model="dataForm.supportCount" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="created">
      <el-input v-model="dataForm.created" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="modified">
      <el-input v-model="dataForm.modified" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
           id: 0,
          title: '',
          cover: '',
          content: '',
          teamid: '',
          commentCount: '',
          collectionCount: '',
          supportCount: '',
          created: '',
          modified: ''
        },
        dataRule: {
          title: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          cover: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          teamid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          commentCount: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          collectionCount: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          supportCount: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          created: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          modified: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm. id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm. id) {
            this.$http({
              url: this.$http.adornUrl(`/news/news/info/${this.dataForm. id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.news.title
                this.dataForm.cover = data.news.cover
                this.dataForm.content = data.news.content
                this.dataForm.teamid = data.news.teamid
                this.dataForm.commentCount = data.news.commentCount
                this.dataForm.collectionCount = data.news.collectionCount
                this.dataForm.supportCount = data.news.supportCount
                this.dataForm.created = data.news.created
                this.dataForm.modified = data.news.modified
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/news/news/${!this.dataForm. id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                ' id': this.dataForm. id || undefined,
                'title': this.dataForm.title,
                'cover': this.dataForm.cover,
                'content': this.dataForm.content,
                'teamid': this.dataForm.teamid,
                'commentCount': this.dataForm.commentCount,
                'collectionCount': this.dataForm.collectionCount,
                'supportCount': this.dataForm.supportCount,
                'created': this.dataForm.created,
                'modified': this.dataForm.modified
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
